package com.edu.zhku.wms.controller;

import com.edu.zhku.wms.common.CommonResult;
import com.edu.zhku.wms.common.Update;
import com.edu.zhku.wms.dto.ModifyPasswordRequest;
import com.edu.zhku.wms.dto.UserQueryDto;
import com.edu.zhku.wms.pojo.User;
import com.edu.zhku.wms.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation("根据主键获取单个用户")
    @Secured("管理员")
    @GetMapping("/selectByPrimaryKey")
    public CommonResult selectByPrimaryKey(@RequestParam Integer id) {
        User user = userService.selectByPrimaryKey(id);

        if (user != null) {
            return CommonResult.success(user);
        } else {
            return CommonResult.error("无此用户");
        }
    }

    @ApiOperation("条件查询用户")
    @Secured("管理员")
    @GetMapping("/selectUser")
    public CommonResult selectUser(UserQueryDto userQueryDto,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return CommonResult.success(userService.list(userQueryDto, pageNum, pageSize));
    }

    @ApiOperation("添加单个用户")
    @Secured("管理员")
    @PostMapping("/addUser")
    public CommonResult addUser(@RequestBody @Validated User user) {
        if (userService.addUser(user) > 0) {
            return CommonResult.success();
        } else {
            return CommonResult.error();
        }
    }

    @ApiOperation("根据主键删除单个用户")
    @Secured("管理员")
    @DeleteMapping("/deleteUserByPrimaryKey")
    public CommonResult deleteUserByPrimaryKey(HttpServletRequest request, @RequestParam Integer id) {
        int i = userService.deleteUserByPrimaryKey(request, id);
        if (i > 0) {
            return CommonResult.success(i);
        } else {
            return CommonResult.error();
        }
    }

    @ApiOperation("修改单个用户信息")
    @Secured("管理员")
    @PutMapping("/updateUser")
    public CommonResult updateUser(HttpServletRequest request, @RequestBody @Validated(Update.class) User user) {
        int i = userService.updateUser(request, user);
        if (i > 0) {
            return CommonResult.success(i);
        } else {
            return CommonResult.error();
        }
    }

    @ApiOperation("修改用户密码")
    @PostMapping("/modifyPassword")
    public CommonResult modifyPassword(@RequestBody @Validated ModifyPasswordRequest modifyPasswordRequest) {
        if (userService.modifyPassword(modifyPasswordRequest) > 0) {
            return CommonResult.success();
        } else {
            return CommonResult.error("原密码输入错误");
        }
    }
}
